package com.example.maple.ccsclient.adapter;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.maple.ccsclient.R;
import com.example.maple.ccsclient.model.PicModel;
import com.example.maple.ccsclient.utils.DisplayUtils;
import com.example.maple.ccsclient.utils.SharedPreferencesUtils;
import com.facebook.drawee.view.SimpleDraweeView;

import java.net.URL;
import java.util.List;

/**
 * Created by maple on 2016/6/20.
 */
public class GridViewAdapter extends ArrayAdapter<PicModel> {

	//是否显示米字格
	boolean isShow;
	LayoutInflater mLayoutInflater = null;
	int mResource;
	Context mContext;
	int mColumn;

	public GridViewAdapter(Context context, int resource, List<PicModel> objects, int column) {
		super(context, resource, objects);
		mResource = resource;
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mColumn = column;
		isShow = SharedPreferencesUtils.getIntersected(context);
	}

	//定义当前GridView是否在滑动状态
	private boolean scrollState = false;

	public void setScrollState(boolean scrollState) {
		this.scrollState = scrollState;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		Log.d("加载位置：", String.valueOf(position));
		ViewHolder holder;
		if (null == convertView) {
			holder = new ViewHolder();
			convertView = mLayoutInflater.inflate(mResource, null);
			// 根据列数计算项目宽度，以使总宽度尽量填充屏幕
			int itemWidth = (mContext.getResources().getDisplayMetrics().widthPixels - (mColumn + 1) * DisplayUtils.dip2px(mContext, 2) - DisplayUtils.dip2px(mContext, 16)) / mColumn;
			// 下面根据比例计算您的item的高度，此处只是使用itemWidth
			int itemHeight = itemWidth;
			AbsListView.LayoutParams param = new AbsListView.LayoutParams(
					itemWidth,
					itemHeight);
			convertView.setLayoutParams(param);
			holder.intersected =  (RelativeLayout) convertView.findViewById(R.id.mizige);
			holder.mImageView = (SimpleDraweeView) convertView.findViewById(R.id.my_image_view);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if(isShow){
			holder.intersected.setVisibility(View.VISIBLE);
		}else{
			holder.intersected.setVisibility(View.GONE);
		}

		if (!scrollState) {
			PicModel info = getItem(position);
			if (null != info && !TextUtils.isEmpty(info.getCImages())) {
				holder.mImageView.setImageURI(Uri.parse(info.getCImages()));
			}
		}

		return convertView;
	}

	static class ViewHolder {
		SimpleDraweeView mImageView;
		public RelativeLayout intersected;
	}

}
